#include<iostream>

using namespace std;

const int N = 1e5 + 10;
int aa[N], h, id;


int main()
{
	int a; cin >> a;
	while (a--)
	{
		int b; cin >> b;
		if (b == 1)
		{
			int x; cin >> x;
			aa[++id] = x;
		}
		else if (b == 2)
		{
			if (h != id)
				h++;
			else
				cout << "ERR_CANNOT_POP" << endl;
		}
		else if (b == 3)
		{
			if (h != id)
				cout << aa[h + 1] << endl;
			else
				cout << "ERR_CANNOT_QUERY" << endl;
		}
		else if (b == 4)
		{
			cout << id - h << endl;
		}
	}


	return 0;
}
